﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LightPersist;
using System.IO;
using LightPersist.Metainfo;
using System.Windows.Markup;
using System.Threading;
using Dom;
using LightPersist.Storage;

namespace LightPersistTest.ComplexUI
{
	/// <summary>
	/// Interaction logic for CollectionPerformance.xaml
	/// </summary>
	public partial class CollectionPerformance : Window
	{
		LightPersistContext Ctx;
		Root Root;
		LightPersist.Transactions.TransactionGuard Transaction;
		Random Rnd;

		public CollectionPerformance()
		{		

			var storage = new InmemoryStorage();
			storage.Connect("what should I write here?");
			Ctx = new LightPersistContext(storage, DomMetainfo.Metainfo);

			using (var transaction = Ctx.BeginTransaction())
			{
				Root = Ctx.RootClass<Root>();

				Root.Clipboard = Ctx.Create<Dom.Clipboard>();


				Rnd = new Random();

				for (int i = 0; i < 10; i++)
				{
					var ctrl = CreateControl(Root.Children, null);			
				}

				InitializeComponent();
				DataContext = Root;
				transaction.CommitTransaction();
			}

			Transaction = Ctx.BeginTransaction();

			//DataContext = new CollectionView(Root.Children);

			this.SourceUpdated += new EventHandler<DataTransferEventArgs>(Window1_SourceUpdated);

		}

		void Window1_SourceUpdated(object sender, DataTransferEventArgs e)
		{

			Debug.WriteLine(sender.ToString() + " args: " + e.ToString());

			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();
		}

		int index = 1;
		Dom.Control CreateControl(IList<Dom.Control> whereToAdd, Dom.Control parent)
		{
			var ctrl = Ctx.Create<Dom.Control>();
			whereToAdd.Add(ctrl);

			ctrl.Name = "Control " + index;
			ctrl.Width = index * 10;
			ctrl.Height = index * 5;
			ctrl.IsVisible = (index & 1) == 1;
			ctrl.Parent = parent;

			index++;
			return ctrl;
		}


		private void deleteCtrol_Click(object sender, RoutedEventArgs e)
		{
			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

			if (listControls.SelectedValue != null)
			{
				var ctrl = (Dom.Control)listControls.SelectedValue;
			
				Root.Children.Remove(ctrl);
				
			}


			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

		}

		private void addCtrl_Click(object sender, RoutedEventArgs e)
		{

			Transaction.CommitTransaction();
			Transaction = Ctx.BeginTransaction();

			var newCtrl = CreateControl(Root.Children, null);
			Root.Children.Add(newCtrl);

			var newCtrl2 = CreateControl(Root.Children, null);
			Root.Children.Add(newCtrl2);

			Transaction.CommitTransaction();

			Transaction = Ctx.BeginTransaction();

		}



	}
}
